package com.transcoachapp.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.transcoachapp.R;
import com.transcoachapp.data.Bus;

	public class AutoCompleteRouteAdapter extends ArrayAdapter<Bus> implements Filterable {
		private Activity context;
		private ArrayList<Bus> items;
		private ArrayList<Bus> itemsAll;
	    private ArrayList<Bus> suggestions;
	    private int viewResourceId;
	    
		public AutoCompleteRouteAdapter(Context context,int viewResourceId ,int textViewResourceId,
				ArrayList<Bus> objects) { 
			super(context, viewResourceId,textViewResourceId,objects);
			this.items = objects;
			this.itemsAll = (ArrayList<Bus>) items.clone();
	        this.suggestions = new ArrayList<Bus>();
	        this.viewResourceId = viewResourceId;
			this.context = (Activity) context;
		//	dbh = new DataBaseHelper(context);
		//	db=dbh.getReadableDatabase();
		//	dbAccess();
		}
		@Override
		 public View getView(int position, View convertView, ViewGroup parent) {
	        View v = convertView;
	        if (v == null) {
	            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            v = vi.inflate(viewResourceId, null);
	        }
	        Bus customer = items.get(position);
	        if (customer != null) {
	            TextView routeNameAndCode = (TextView) v.findViewById(R.id.autoCompleteRouteName);
	            if (routeNameAndCode != null) {
//	              Log.i(MY_DEBUG_TAG, "getView Customer Name:"+customer.getName());
	            	routeNameAndCode.setText(customer.getRouteCode()+" - "+customer.getRouteName());
	            }
	        }
	        return v;
	    }

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (this.items != null)
			      return this.items.size();
			return 0;
		}
		@Override
		public Bus getItem(int arg0) {
			// TODO Auto-generated method stub
			return this.items.get(arg0);
		}
		@Override
	    public Filter getFilter() {
	        return nameFilter;
	    }

	    Filter nameFilter = new Filter() {
	        public String convertResultToString(Object resultValue) {
	            String str = ((Bus)(resultValue)).getRouteCode() +"-"+ ((Bus)(resultValue)).getRouteName();
	            return str;
	        }
	        @Override
	        protected FilterResults performFiltering(CharSequence constraint) {
	            if(constraint != null) {
	                suggestions.clear();
	                for (Bus bus : itemsAll) {
	                    if(bus.getRouteCode().toLowerCase().startsWith(constraint.toString().toLowerCase()) || bus.getRouteName().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
	                        suggestions.add(bus);
	                    }
	                }
	                FilterResults filterResults = new FilterResults();
	                filterResults.values = suggestions;
	                filterResults.count = suggestions.size();
	                return filterResults;
	            } else {
	                return new FilterResults();
	            }
	        }
	        @Override
	        protected void publishResults(CharSequence constraint, FilterResults results) {
	        	
	        	ArrayList<Bus> filteredList = (ArrayList<Bus>) results.values;
	            if(results != null) {
	            	clear();
	            	if (results.count > 0) {
		                for (Bus c : filteredList) {
		                    add(c);
		                }
		                notifyDataSetChanged();
	                
	                }else {	
	                	notifyDataSetInvalidated();
	                }
	            	}
	        }
	    };

}
